package com.example.instrument.service.Impl;

import com.example.instrument.dao.InstrumentMapper;
import com.example.instrument.service.InstrumentService;
import com.example.instrument.vo.InstrumentVo;
import com.example.result.CommonResult;
import com.example.utils.MyPageHelper;
import com.example.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class InstrumentServiceImpl implements InstrumentService {
    @Autowired
    InstrumentMapper instrumentMapper;
    /**
     * 收费管理-仪表全查
     * @param instrumentName
     * @return
     */
    @Override
    public CommonResult getInstrument(String instrumentName) {
        return CommonResult.ok(MyPageHelper.getPageResult(instrumentMapper.getInstrument(instrumentName)));
    }

    /**
     *  收费管理-仪表删除
     * @param id
     * @return
     */
    @Override
    public int delInstrument(Integer id) {
        int i = instrumentMapper.delInstrument(id);
        return i;
    }

    /**
     * 仪表增改
     * @param instrumentVo
     * @return
     */
    @Override
    public CommonResult saveOrUpdateInstrument(InstrumentVo instrumentVo) {
        int i = 0;
        if(instrumentVo.getId() == 0){
            int num = NumUtil.Num();
            String code = "YB" + num;
            instrumentVo.setInstrumentCode(code);
            instrumentVo.setInstrumentState(0);
            instrumentVo.setInstrumentReportingId(0);
            instrumentVo.setCreateTime(new Date());
            instrumentVo.setUpdateTime(new Date());
            instrumentVo.setDelFlag(0);
            i = instrumentMapper.addInstrument(instrumentVo);
        }else {
            instrumentVo.setUpdateTime(new Date());
            i = instrumentMapper.updateInstrument(instrumentVo);
        }
        if(i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail();
        }
    }
}
